package cn.stylefeng.guns.modular.business.timer.delay;

import cn.stylefeng.guns.modular.business.frequency.orderfinish.model.param.OrderSubDelayVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Iterator;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.ExecutorService;

/**
 * @Description:
 * @Author jzy
 * @Date 2021/11/26
 **/
@Slf4j
@Component
public class DelayQueueManager implements DelayOrder<OrderSubDelayVO> {

    @Autowired
    private ExecutorService delayOrderExecutor;
    //@Resource
    //private AppOrderService appOrderService;

    private final static DelayQueue<DelayTask<OrderSubDelayVO>> DELAY_QUEUE = new DelayQueue<>();

    /**
     * 初始化时加载数据库中需处理超时的订单
     * 系统启动:扫描数据库中未支付(要在更新时:加上已支付就不用更新了),未过期的的订单
     */
    @PostConstruct
    public void init() {
        log.info("系统启动:扫描数据库中未确认到达,未过期的的订单");
        //List<OrderSubDelayVO> orderList = appOrderService.selectFutureOverTimeOrder();
        //for (OrderSubDelayVO order : orderList) {
        //    DelayTask<OrderSubDelayVO> orderDelayed = new DelayTask<>(order.getOrderId(), order.getStartTime());
        //    this.addToOrderDelayQueue(orderDelayed);
        //}
        //log.info("系统启动:扫描数据库中未确认到达的订单,总共扫描了" + orderList.size() + "个订单,推入检查队列,准备到期检查...");

        /*启动一个线程，去取延迟订单*/
        delayOrderExecutor.execute(() -> {
            log.info("启动处理的订单线程:" + Thread.currentThread().getName());
            while (true) {
                try {
                    DelayTask<OrderSubDelayVO> orderDelayed = DELAY_QUEUE.take();
                    //处理超时订单
                    //appOrderService.updateCloseOverTimeOrder(orderDelayed.getDataId(), orderDelayed.getStartTime());
                } catch (Exception e) {
                    log.error("执行自营超时订单的_延迟队列_异常:" + e);
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 加入延迟消息队列
     **/
    @Override
    public boolean addToOrderDelayQueue(DelayTask<OrderSubDelayVO> orderDelayed) {
        return DELAY_QUEUE.add(orderDelayed);
    }

    /**
     * 加入延迟消息队列
     **/
    @Override
    public boolean addToDelayQueue(OrderSubDelayVO order) {
        DelayTask<OrderSubDelayVO> orderDelayed = new DelayTask<OrderSubDelayVO>(order.getOrderId().toString(), order.getStartTime().getTime());
        return DELAY_QUEUE.add(orderDelayed);
    }

    /**
     * 从延迟队列中移除 主动取消就主动从队列中取出
     **/
    @Override
    public void removeToOrderDelayQueue(OrderSubDelayVO order) {
        if (order == null) {
            return;
        }
        for (Iterator<DelayTask<OrderSubDelayVO>> iterator = DELAY_QUEUE.iterator(); iterator.hasNext(); ) {
            DelayTask<OrderSubDelayVO> queue = iterator.next();
            if (queue.getDataId().equals(order.getOrderId())) {
                DELAY_QUEUE.remove(queue);
            }
        }
    }
}
